<template>
	<view>
		<button v-if="!isAuthrization" class="button" @getuserinfo="getUserInfo()" @tap="getUserInfo()">微信授权登录</button>
	</view>
</template>

<script>
	import { wxLogin } from '@/common/api/quickRegister.js';
	import  { inputCheck } from '@/common/js/inputCheck.js';
	import { setToken, getToken } from '@/common/utils/auth.js'
	import md5 from 'js-md5';
	import {error} from '@/common/js/errorTips.js'
	import {getUserCardInfo} from '@/common/api/userInfo.js'
	import WXBizDataCrypt from '@/common/utils/WXBizDataCrypt.js'
	import { getOpenId } from '@/common/api/config.js';
	import { getCode } from '../../../../common/utils/getCode';


	export default {
		data() {
			return {
				encryptedData: '',
				isVisible: false,
				isRegisterVisited: false,
				isPasswordVisited: false,
				isAuthrization: uni.getStorageSync("isAuthrization")
			}
		},
		methods: {
			// 授权获取用户信息
			getUserInfo: function() {
				//this.wxLogin()
				this.weChat()
			},
			// 微信小程序的授权
			weChat: function() {
				uni.showLoading({
					title: '加载中'
				})
				let that = this
				wx.getSetting({
					success(res) {
						if (res.authSetting['scope.userInfo']) {
							//获取用户头像和昵称
							wx.getUserProfile({
								desc:'用于完善用户资料',
								success: (res) =>{
								//console.log('获取用户信息',res)
								// that.encryptedData = res.encryptedData
								// that.iv = res.iv
								// 将用户头像和昵称存入缓存
								uni.setStorageSync('avatarUrl', res.userInfo.avatarUrl)
								uni.setStorageSync('username',res.userInfo.nickName)
								//获取openId
								//已经授权
								that.isAuthrization = true
								uni.hideLoading()
								that.wxLogin()
								return true
							},
							fail: () => {
								console.log("微信登录失败")
								uni.hideLoading()
								return false
							}
							})
						}
					}
				})
			},
			wxLogin:function(){
				let that = this
				// 获取用户openid
				let code = uni.getStorageSync('code')
				getOpenId(code).then(res => {
					console.log('openid',res)
					if(res.data.code === 200){
						uni.setStorageSync('openid', res.data.data);
						uni.showToast({
							title: '授权登录成功',
							icon: 'success'
						})
						// 微信授权登录成功1s后跳转填写信息界面
						setTimeout(function() {
							//uni.navigateBack()
							uni.navigateTo({
								url:'/pagesA/pages/aboutMe/aboutMe'
							})
						}, 1000)
					}
				})  
			},
			
		
		},
		onLoad() {
			getCode();
		}

	}
</script>

<style lang="scss">
	@import '@/common/scss/form.scss';
	@import '@/common/scss/common.scss';

	page {
		width: 100%;
		height: 100%;
		background-color: #F2F2F2;
	}

	@mixin mt($top) {
		margin-top: $top;
	}

	.input-inbox {
		@extend .input-box;
		@include mt(80rpx);
		display: flex;
		align-items: center;

		.input {
			@include width-margin(90%, 90%);
		}
	}

	.password-row-box {
		@extend .password-box;
		align-items: center;
		@include mt(40rpx);
		display: flex;
		align-items: center;

		.input {
			width: 83%;
			height: 90%;
			margin-left: 5%;
		}

		.eye-icon {
			width: 17%;
			@include icon-size(50rpx, 50rpx);
		}
	}

	// 登录按钮
	.button {
		background-color: #754c24;
		@extend .button-box;
		@include mt(50rpx);
	}

	// 选择注册和修改密码
	.enroll-changepassword-box {
		@include width-margin(90%, 70rpx);
		margin-top: 40rpx;
		@include flex-direction(row);
		@include row-left-right(50%, 50%);
	}
</style>
